import Sequelize from 'sequelize';
import mysqlConfig from '../../config/mysql.config';  //引入数据库配置模块中的数据

const sequelize = new Sequelize(mysqlConfig.database, mysqlConfig.user, mysqlConfig.password, {
  host: mysqlConfig.host, // 连接地址
  dialect: mysqlConfig.dialect, // 数据库类型
  timezone: "+08:00", // 数据库时间相关操作时区
  define: {
    timestamps: true // `timestamps` 字段指定是否将创建 `createdAt` 和 `updatedAt` 字段.
  }
});

function query(sql, params, callback) {
  sequelize
    .authenticate()
    .then(() => {
      console.log('Connection has been established successfully.');
    }).catch(err => {
      console.error('Unable to connect to the database:', err);
    }).finally(() => {
      console.log('close!!!')
    });
}

function closeCon() {
  sequelize.close()
}

//向外暴露
export default {
  sequelize,
  query,
  closeCon,
}